added some development tools
[windows-sources.git] / developer / Samples / NET 4.6 / Samples for Parallel / Backup / NBodySimulation / NBodySimulationBackend / Globals.fs
blob639a3f1b3bcaf222794baa84f64e392082d68095
1 //--------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // File: globals.fs
6 //
7 //--------------------------------------------------------------------------
9 module Globals
11 open Microsoft.FSharp.Math
13 open System.Windows.Media
14 open System.ComponentModel
15 open System
16 open System.Threading.Tasks
18 let epsilonValue = 1.0e-2
19 let nominalMass=1.0e-9<SI.kg>
20 let gravity = PhysicalConstants.G
22 let mutable gNumParticles = 128
24 let timeStepSize = 100.0<SI.s>
26 let drawParticleSizeScale = 0.2
27 let particleSystemCentroid = -1.0/1.0e8<SI.s>
29 let defaultFocalColor = Colors.White
31 let mutable gNumCoresUsed = Environment.ProcessorCount
32 let op = new ParallelOptions()
33 op.MaxDegreeOfParallelism <- gNumCoresUsed
35 let mutable rand = new System.Random(4)
36 let rd() = (rand.NextDouble())
37 let rs() = (2.0 * ((rand.NextDouble()) - 0.5)) // random number between -1, 1
38 let qPosRnd () = rand.NextDouble()
39 let qNegRnd () = -1.0 * rand.NextDouble() // random number between -1, 0
41 type particle =
42 struct
43 val mutable px: float<SI.m>
44 val mutable py: float<SI.m>
45 val mutable vx: float<SI.m/SI.s>
46 val mutable vy: float<SI.m/SI.s>
48 val mutable mass: float<SI.kg>
49 new(xpos:float<SI.m>, ypos:float<SI.m>,
50 xvel:float<SI.m/SI.s>, yvel:float<SI.m/SI.s>,
51 m:float<SI.kg>)
53 { px = xpos;
54 py = ypos;
55 vx = xvel;
56 vy = yvel;
57 mass = m }
58 end
60 [<Struct>]
61 type point<[<Measure>]'u> =
62 val mutable x: float<'u>
63 val mutable y: float<'u>
64 new(newx,newy) = { x = newx; y = newy}
65 static member (-) (p1:point<'u>, p2:point<'u>) = new point<'u>(p1.x-p2.x, p1.y-p2.y)
67 type quadrants =
68 | I = 0
69 | II = 1
70 | III = 2
71 | IV = 3
72 | Root = 4
74 /// Returns the enum quadrants values in an array
75 let enumeratedQuadrantsArray =
76 seq { for i in 0..3 do yield enum<quadrants>(i)}
77 |> Seq.toArray
79 let spawnUniformly quadrantNumber =
80 let initVel = 0.0<SI.m/SI.s>
81 match quadrantNumber with
82 | quadrants.I ->particle( qPosRnd()*1.0<SI.m>, qPosRnd()*1.0<SI.m>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
83 | quadrants.II -> particle( qNegRnd()*1.0<SI.m>,qPosRnd()*1.0<SI.m>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
84 | quadrants.III -> particle( qNegRnd()*1.0<SI.m>,qNegRnd()*1.0<SI.m>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
85 | quadrants.IV -> particle( qPosRnd()*1.0<SI.m>,qNegRnd()*1.0<SI.m>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
86 | _ -> failwithf "either you sent a root, or unknown quadrant matched"
88 let fillParticleArray i =
89 match i with
90 | x when x < gNumParticles/4 -> spawnUniformly quadrants.I
91 | x when x < gNumParticles/2 -> spawnUniformly quadrants.II
92 | x when x < (3*gNumParticles)/4 -> spawnUniformly quadrants.III
93 | x when x <= gNumParticles -> spawnUniformly quadrants.IV
95 let mutable globalState = Array.init gNumParticles (fun i -> fillParticleArray i)
97 let restart() =
98 rand <- new System.Random(4)
99 globalState <- Array.mapi( fun i ele -> fillParticleArray i) globalState
101 let changeNumberOfParticles newNumber =
102 rand <- new System.Random(4)
103 gNumParticles <- newNumber
104 globalState <- Array.init newNumber (fun i -> fillParticleArray i)
105 GC.Collect()
107 let globalParticleList (arr:particle array) =
108 List.ofArray arr